/*
 * @Author: ✨Fireflx 
 * @Date: 2022-02-16 09:06:22 
 * @Last Modified by: ✨Fireflx
 * @Last Modified time: 2022-03-03 11:11:45
 */

const { Article } = require('../models')

const artAdd = async ctx => {
    const { body: article } = ctx.request;
    await Article.create(article)
        .then(res => {
            ctx.body = res ? {
                code: 200,
                msg: '创建成功！',
            } : {
                code: 300,
                msg: "创建失败"
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '创建异常！',
                err,
            }
        })
}

const find = async ctx => {
    //可传入每页几条
    let { page, author } = ctx.query
    //对查询第几页进行兜底
    if (!page || isNaN(Number(page))) {
        page = 1
    } else {
        page = Number(page)
    }
    //每页条数
    const pageSize = 4;
    //计算总页数
    let artCount = 0;
    await Article.find({ author }).count()//🌟
        .then(res => {
            artCount = res
        })
    let pageCount = 0;
    if (artCount > 0) {
        pageCount = Math.ceil(artCount / pageSize)
    }
    //判断当前页码范围
    if (pageCount > 0 && page > pageCount) {
        page = pageCount
    } else if (page < 1) {
        page = 1
    }
    //计算起始位置
    const start = (page - 1) * pageSize;

    await Article.find({ author }).skip(start).limit(pageSize)//🌟//🌟
        .then(res => {
            ctx.body = (res && res.length > 0) ? {
                code: 200,
                msg: '查询成功！',
                page,
                pageSize,
                artCount,
                data: res
            } : {
                code: 300,
                msg: "查询失败"
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '查询异常！',
                err,
            }
        })

}

const findOne = async ctx => {
    const { id } = ctx.query;
    let readed = false
    await Article.findOne({ id })
        .then(res => {
            readed = res ? true :false;
            ctx.body = res ? {
                code: 200,
                msg: '查询成功！',
                data: res,
            } : {
                code: 300,
                msg: "查询失败"
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '查询异常！',
                err,
            }
        })
    if(readed) {
        await Article.updateOne({id},{$inc:{read:1}})
    }
}

const artUp = async ctx => {
    const { body: article, body: { id } } = ctx.request;
    delete article.id
    await Article.updateOne({ id }, article)
        .then(res => {
            ctx.body = res.modifiedCount ? {
                code: 200,
                msg: "修改文章成功！",
            } : {
                code: 300,
                msg: '已更改零项文章信息！'
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '修改文章失败！',
            }
        })
}

const artDel = async ctx => {
    const { id } = ctx.request.body
    await Article.findOneAndDelete({ id })
        .then(res => {
            ctx.body = res ? {
                code: 200,
                msg: '删除成功！',
            } : {
                code: 300,
                msg: "删除失败"
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '删除异常！',
                err,
            }
        })
}

module.exports = {
    artAdd,
    find,
    findOne,
    artUp,
    artDel
}